/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.text.representation.io;

import co.edu.unal.bioingenium.kbmed.config.Configuration;
import co.edu.unal.bioingenium.kbmed.text.representation.vo.WordList;
import co.edu.unal.bioingenium.kbmed.util.io.FileManager;
import gnu.trove.map.TObjectIntMap;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alejandro Riveros Cruz
 */
public class WordListIO {

    /**
     *
     *
     * @param wordList 
     * @param name 
     * @throws IOException 
     */
    public static void storePlainWordList(WordList wordList, String name) throws IOException {
        BufferedWriter bufferedWriter = null;
        try {
            String path = Configuration.EXPERIMENTS_DIR + File.separator + FileManager.getStandardName(name) + "PlainWordList.txt";
            bufferedWriter = new BufferedWriter(new FileWriter(path));
            TObjectIntMap<String> tempInObjectOcurrences = wordList.getWordListInObjectOcurrences();
            TObjectIntMap<String> tempTotalOcurrences = wordList.getWordListTotalOcurrences();
            bufferedWriter.write("index,word,InObjectOcurrences,TotalOcurrences\n");
            int i = 0;
            for (String word : tempInObjectOcurrences.keySet()) {
                bufferedWriter.write((i++) + "," + word + "," + tempInObjectOcurrences.get(word) + "," + tempTotalOcurrences.get(word));
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (IOException ioex) {
            Logger.getLogger(CorpusIO.class.getName()).log(Level.SEVERE, null, ioex);
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(CorpusIO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     *
     *
     * @param WordList 
     * @param name 
     * @throws IOException 
     */
    public static void storeWordList(WordList wordList, String name) throws IOException {
        File file = new File(Configuration.DATA_DIR + File.separator + name);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(wordList);
        objectOutputStream.close();
    }

    /**
     *
     *
     * @param name
     * @return 
     * @throws IOException
     * @throws ClassNotFoundException  
     */
    public static WordList loadWordList(String name) throws IOException, ClassNotFoundException {
        FileInputStream fileInputStream = new FileInputStream(Configuration.DATA_DIR + File.separator + name);
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        WordList wordList = (WordList) objectInputStream.readObject();
        objectInputStream.close();
        return wordList;
    }
}
